home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zstack.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.8 KB  |  302 lines

  1. /* Copyright (C) 1989, 1991, 1992, 1994, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zstack.c,v 1.2 2000/09/19 19:00:55 lpd Exp $ */
  20. /* Operand stack operators */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "ialloc.h"
  24. #include "istack.h"
  25. #include "oper.h"
  26. #include "store.h"
  27.  
  28. /* <obj> pop - */
  29. int
  30. zpop(i_ctx_t *i_ctx_p)
  31. {
  32.     os_ptr op = osp;
  33.  
  34.     check_op(1);
  35.     pop(1);
  36.     return 0;
  37. }
  38.  
  39. /* <obj1> <obj2> exch <obj2> <obj1> */
  40. int
  41. zexch(i_ctx_t *i_ctx_p)
  42. {
  43.     os_ptr op = osp;
  44.     ref next;
  45.  
  46.     check_op(2);
  47.     ref_assign_inline(&next, op - 1);
  48.     ref_assign_inline(op - 1, op);
  49.     ref_assign_inline(op, &next);
  50.     return 0;
  51. }
  52.  
  53. /* <obj> dup <obj> <obj> */
  54. int
  55. zdup(i_ctx_t *i_ctx_p)
  56. {
  57.     os_ptr op = osp;
  58.  
  59.     check_op(1);
  60.     push(1);
  61.     ref_assign_inline(op, op - 1);
  62.     return 0;
  63. }
  64.  
  65. /* <obj_n> ... <obj_0> <n> index <obj_n> ... <obj_0> <obj_n> */
  66. int
  67. zindex(i_ctx_t *i_ctx_p)
  68. {
  69.     os_ptr op = osp;
  70.     register os_ptr opn;
  71.  
  72.     check_type(*op, t_integer);
  73.     if ((ulong)op->value.intval >= op - osbot) {
  74.     /* Might be in an older stack block. */
  75.     ref *elt;
  76.  
  77.     if (op->value.intval < 0)
  78.         return_error(e_rangecheck);
  79.     elt = ref_stack_index(&o_stack, op->value.intval + 1);
  80.     if (elt == 0)
  81.         return_error(e_rangecheck);
  82.     ref_assign(op, elt);
  83.     return 0;
  84.     }
  85.     opn = op + ~(int)op->value.intval;
  86.     ref_assign_inline(op, opn);
  87.     return 0;
  88. }
  89.  
  90. /* <obj_n-1> ... <obj_0> <n> <i> roll */
  91. /*      <obj_(i-1)_mod_ n> ... <obj_0> <obj_n-1> ... <obj_i_mod_n> */
  92. int
  93. zroll(i_ctx_t *i_ctx_p)
  94. {
  95.     os_ptr op = osp;
  96.     os_ptr op1 = op - 1;
  97.     int count, mod;
  98.     register os_ptr from, to;
  99.     register int n;
  100.  
  101.     check_type(*op1, t_integer);
  102.     check_type(*op, t_integer);
  103.     if ((ulong) op1->value.intval > op1 - osbot) {
  104.     /*
  105.      * The data might span multiple stack blocks.
  106.      * There are efficient ways to handle this situation,
  107.      * but they're more complicated than seems worth implementing;
  108.      * for now, do something very simple and inefficient.
  109.      */
  110.     int left, i;
  111.  
  112.     if (op1->value.intval < 0 ||
  113.         op1->value.intval + 2 > ref_stack_count(&o_stack)
  114.         )
  115.         return_error(e_rangecheck);
  116.     count = op1->value.intval;
  117.     if (count <= 1) {
  118.         pop(2);
  119.         return 0;
  120.     }
  121.     mod = op->value.intval;
  122.     if (mod >= count)
  123.         mod %= count;
  124.     else if (mod < 0) {
  125.         mod %= count;
  126.         if (mod < 0)
  127.         mod += count;    /* can't assume % means mod! */
  128.     }
  129.     /* Use the chain rotation algorithm mentioned below. */
  130.     for (i = 0, left = count; left; i++) {
  131.         ref *elt = ref_stack_index(&o_stack, i + 2);
  132.         ref save;
  133.         int j, k;
  134.         ref *next;
  135.  
  136.         save = *elt;
  137.         for (j = i, left--;; j = k, elt = next, left--) {
  138.         k = (j + mod) % count;
  139.         if (k == i)
  140.             break;
  141.         next = ref_stack_index(&o_stack, k + 2);
  142.         ref_assign(elt, next);
  143.         }
  144.         *elt = save;
  145.     }
  146.     pop(2);
  147.     return 0;
  148.     }
  149.     count = op1->value.intval;
  150.     if (count <= 1) {
  151.     pop(2);
  152.     return 0;
  153.     }
  154.     mod = op->value.intval;
  155.     /*
  156.      * The elegant approach, requiring no extra space, would be to
  157.      * rotate the elements in chains separated by mod elements.
  158.      * Instead, we simply check to make sure there is enough space
  159.      * above op to do the roll in two block moves.
  160.      * Unfortunately, we can't count on memcpy doing the right thing
  161.      * in *either* direction.
  162.      */
  163.     switch (mod) {
  164.     case 1:        /* common special case */
  165.         pop(2);
  166.         op -= 2;
  167.         {
  168.         ref top;
  169.  
  170.         ref_assign_inline(&top, op);
  171.         for (from = op, n = count; --n; from--)
  172.             ref_assign_inline(from, from - 1);
  173.         ref_assign_inline(from, &top);
  174.         }
  175.         return 0;
  176.     case -1:        /* common special case */
  177.         pop(2);
  178.         op -= 2;
  179.         {
  180.         ref bot;
  181.  
  182.         to = op - count + 1;
  183.         ref_assign_inline(&bot, to);
  184.         for (n = count; --n; to++)
  185.             ref_assign(to, to + 1);
  186.         ref_assign_inline(to, &bot);
  187.         }
  188.         return 0;
  189.     }
  190.     if (mod < 0) {
  191.     mod += count;
  192.     if (mod < 0) {
  193.         mod %= count;
  194.         if (mod < 0)
  195.         mod += count;    /* can't assume % means mod! */
  196.     }
  197.     } else if (mod >= count)
  198.     mod %= count;
  199.     if (mod <= count >> 1) {
  200.     /* Move everything up, then top elements down. */
  201.     if (mod >= ostop - op) {
  202.         o_stack.requested = mod;
  203.         return_error(e_stackoverflow);
  204.     }
  205.     pop(2);
  206.     op -= 2;
  207.     for (to = op + mod, from = op, n = count; n--; to--, from--)
  208.         ref_assign(to, from);
  209.     memcpy((char *)(from + 1), (char *)(op + 1), mod * sizeof(ref));
  210.     } else {
  211.     /* Move bottom elements up, then everything down. */
  212.     mod = count - mod;
  213.     if (mod >= ostop - op) {
  214.         o_stack.requested = mod;
  215.         return_error(e_stackoverflow);
  216.     }
  217.     pop(2);
  218.     op -= 2;
  219.     to = op - count + 1;
  220.     memcpy((char *)(op + 1), (char *)to, mod * sizeof(ref));
  221.     for (from = to + mod, n = count; n--; to++, from++)
  222.         ref_assign(to, from);
  223.     }
  224.     return 0;
  225. }
  226.  
  227. /* |- ... clear |- */
  228. /* The function name is changed, because the IRIS library has */
  229. /* a function called zclear. */
  230. private int
  231. zclear_stack(i_ctx_t *i_ctx_p)
  232. {
  233.     ref_stack_clear(&o_stack);
  234.     return 0;
  235. }
  236.  
  237. /* |- <obj_n-1> ... <obj_0> count <obj_n-1> ... <obj_0> <n> */
  238. private int
  239. zcount(i_ctx_t *i_ctx_p)
  240. {
  241.     os_ptr op = osp;
  242.  
  243.     push(1);
  244.     make_int(op, ref_stack_count(&o_stack) - 1);
  245.     return 0;
  246. }
  247.  
  248. /* - mark <mark> */
  249. private int
  250. zmark(i_ctx_t *i_ctx_p)
  251. {
  252.     os_ptr op = osp;
  253.  
  254.     push(1);
  255.     make_mark(op);
  256.     return 0;
  257. }
  258.  
  259. /* <mark> ... cleartomark */
  260. int
  261. zcleartomark(i_ctx_t *i_ctx_p)
  262. {
  263.     uint count = ref_stack_counttomark(&o_stack);
  264.  
  265.     if (count == 0)
  266.     return_error(e_unmatchedmark);
  267.     ref_stack_pop(&o_stack, count);
  268.     return 0;
  269. }
  270.  
  271. /* <mark> <obj_n-1> ... <obj_0> counttomark */
  272. /*      <mark> <obj_n-1> ... <obj_0> <n> */
  273. private int
  274. zcounttomark(i_ctx_t *i_ctx_p)
  275. {
  276.     os_ptr op = osp;
  277.     uint count = ref_stack_counttomark(&o_stack);
  278.  
  279.     if (count == 0)
  280.     return_error(e_unmatchedmark);
  281.     push(1);
  282.     make_int(op, count - 1);
  283.     return 0;
  284. }
  285.  
  286. /* ------ Initialization procedure ------ */
  287.  
  288. const op_def zstack_op_defs[] =
  289. {
  290.     {"0clear", zclear_stack},
  291.     {"0cleartomark", zcleartomark},
  292.     {"0count", zcount},
  293.     {"0counttomark", zcounttomark},
  294.     {"1dup", zdup},
  295.     {"2exch", zexch},
  296.     {"2index", zindex},
  297.     {"0mark", zmark},
  298.     {"1pop", zpop},
  299.     {"2roll", zroll},
  300.     op_def_end(0)
  301. };
  302.